const router = require('koa-router')()
const calendarService = require('../service/calendar');
const jwt = require('jsonwebtoken');

/**
 * @function /calendar/getProcess
 * @type GET
 * @param {time} 可选, 有则搜索特定时间的所有行程
 * @description { 获取行程列表 }
 * @returns { result } 
 */
router.get('/getProcess', async(ctx) => {
    let time = ctx.query.time === undefined ? 0 : ctx.query.time
    const res = await calendarService.getProcess(time);
    try {
        ctx.body = {
            status: res.status,
            data: res.data,
            msg: res.msg
        }
    } catch (e) {
        console.log('error: ', e)
        ctx.body = {
            code: 500,
            data: "Server Error, Please contact with server"
        }
    }
})


/**
 * @function /calendar/addProcess
 * @type POST
 * @param { time } 时间
 * @param { process } 行程内容
 * @param { notify } 是否提醒
 * @description { 新增行程 }
 * @returns { result } 
 */
router.post('/addProcess', async(ctx) => {
    if (
        ctx.request.body.process === undefined ||
        ctx.request.body.time === undefined ||
        ctx.request.body.notify === undefined
    ) {
        ctx.body = {
            status: 401,
            data: [],
            msg: `缺少参数`
        }
    } else {
        const process = ctx.request.body.process
        const time = ctx.request.body.time
        const notify = ctx.request.body.notify
        const res = await calendarService.addProcess(process, time, notify);
        try {
            ctx.body = {
                status: res.status,
                data: res.data,
                msg: res.msg
            }
        } catch (e) {
            console.log('error: ', e)
            ctx.body = {
                code: 500,
                data: "Server Error, Please contact with server"
            }
        }
    }

})



/**
 * @function /calendar/changeNotifyProcess
 * @type POST
 * @param { id } 行程id
 * @param { notify } 行程提醒状态
 * @description { 修改行程为提醒/不提醒状态 }
 * @returns { result } 
 */
router.post('/changeNotifyProcess', async(ctx) => {
    const id = ctx.request.body.id
    const notify = ctx.request.body.notify === undefined ? 0 : ctx.request.body.notify
    const res = await calendarService.changeNotifyProcess(id, notify);
    try {
        ctx.body = {
            status: res.status,
            data: res.data,
            msg: res.msg
        }
    } catch (e) {
        console.log('error: ', e)
        ctx.body = {
            code: 500,
            data: "Server Error, Please contact with server"
        }
    }
})




/**
 * @function /calendar/deleteProcess
 * @type DELETE
 * @param { id } 行程id
 * @description { 删除行程 }
 * @returns { result } 
 */
router.delete('/deleteProcess', async(ctx) => {
    const id = ctx.request.body.id
    const res = await calendarService.deleteProcess(id);
    try {
        ctx.body = {
            status: res.status,
            data: res.data,
            msg: res.msg
        }
    } catch (e) {
        console.log('error: ', e)
        ctx.body = {
            code: 500,
            data: "Server Error, Please contact with server"
        }
    }
})

module.exports = router